//
//  HMTumourTableViewController.m
//  仿真开发
//
//  Created by jieChen on 17/3/24.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMTumourTableViewController.h"
#import "HMTool.h"
#import <YYModel.h>
#import "AppointModel.h"
#import "MJRefresh.h"

@interface HMTumourTableViewController () 

@property (nonatomic, strong)NSMutableArray *dataArr;

@end
static NSString *identifier = @"cell";
@implementation HMTumourTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //刷新指定时间完成
        [NSThread sleepForTimeInterval:1.3];
        [self.tableView.mj_header endRefreshing];
        
    }];
    
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //刷新指定时间完成
        [NSThread sleepForTimeInterval:1.3];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
    _dataArr = [NSMutableArray array];
    [self loadData];
    
   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
}



//-(void)setDidCure:(NSArray *)didCure{
//    _didCure = didCure;
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
//    [self.tableView reloadData];
//}

- (void) loadData {
    NSString *tagStr = [NSString stringWithFormat:@"%zd",_tag + 1];
    
//    int tagNum = [tagStr intValue];
    NSLog(@"-----%zd",tagStr);
    NSString *url = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id": tagStr,@"keyword":@""};
   
    
    [[HMTool sharedNetworkTool]POSTWithURLString:url parameters:parameters success:^(id responseObject) {
        
       NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
//        NSLog(@"%@",responseObject);
        
        NSArray *data = dic[@"data"];
//        NSLog(@"%@",data);
        
        NSArray *modelArr = [NSArray yy_modelArrayWithClass:[AppointModel class] json:data];
//        NSLog(@"%@",modelArr);
        _dataArr = [NSMutableArray arrayWithArray:modelArr];
       
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
  
    } faile:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    // 将数据模型传递给cell 让cell更新UI
    AppointModel *model = _dataArr[indexPath.row];
    cell.textLabel.text = model.ci3_name;
    return cell;
}

#pragma mark - tableView 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 给模型赋值
    AppointModel *model = _dataArr[indexPath.row];
    
    // 添加代理,点到那个cell的文字  返回出去显示相同文字
    [self.delegate HMTumourTableViewController:self illName:model.ci3_name];
    
    [self.navigationController popViewControllerAnimated:YES];
   
    
//    if ([model.ci3_name isEqualToString:@"胃恶性肿瘤"]) {
    // 添加代理,把模型的名字传给疾病类型的控制器
    [self.delegate HMTumourTableViewController:self Tag:model.ci3_name];
//    }
    
    [self.tableView reloadData];
    
    
}

@end
